package com.cooqe.collection_snmp.snmpfinder;

import com.cooqe.collection_snmp.model.SnmpParameter;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/14-2023
 * @Description: snmp get 方式进行指标的采集
 */
@Component
public class SnmpGetHandle {

    public void snmpGet(String oids, SnmpParameter snmpParameter, Target target, Snmp snmp) throws Exception {
        String[] arrays = oids.split(",");
        for (String oid : arrays) {
            PDU pdu = (snmpParameter.getVersion() == SnmpConstants.version3) ? (new ScopedPDU()) : (new PDU());
            pdu.add(new VariableBinding(new OID(oid)));
            pdu.setType(PDU.GET);

            ResponseEvent responseEvent = snmp.send(pdu, target);
            if (responseEvent == null) {
                throw new RuntimeException("responseEvent is null error");
            }
            if (responseEvent.getResponse() == null) {
                throw new RuntimeException("responseEvent is null error");
            }
            if (responseEvent.getError() != null) {
                throw responseEvent.getError();
            }
            if (responseEvent.getResponse().getErrorStatus() != PDU.noError) {
                throw new RuntimeException("responseEvent.getResponse().getErrorStatus() is error");
            }

            List<? extends VariableBinding> variableBindings = responseEvent.getResponse().getVariableBindings();
        }
    }
}